<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getActivityRemarks">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="50">
			</el-table-column>
			<el-table-column prop="name" label="名字" width="120" sortable>
			</el-table-column>
      <el-table-column prop="price" label="价格" width="80" sortable>
      </el-table-column>
      <el-table-column prop="description" label="描述" width="80" sortable>
      </el-table-column>
			<el-table-column prop="resource" label="资源" width="200" sortable>
			</el-table-column>
			<el-table-column prop="type.name" label="类型" width="200" sortable>
			</el-table-column>
			<!--<el-table-column prop="sex" label="性别" width="100" :formatter="formatSex" sortable>
			</el-table-column>-->
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="query.pageSize" :total="pageInfo.total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增或编辑界面
		v-model="saveFormVisible" 老版本用
		:visible.sync 新版本用
		-->
		<el-dialog title="新增或编辑" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="activityRemark" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="名字" prop="name">
					<el-input v-model="activityRemark.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="优惠方式">
          <el-input v-model="activityRemark.preferential_method_id" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="开始时间" prop="start_date">
          <el-input v-model="activityRemark.start_date" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="结束时间" prop="end_date">
          <el-input v-model="activityRemark.end_date" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="activityRemark.description" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="创建时间时间" prop="create_time">
          <el-input v-model="activityRemark.create_time" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="创建人" prop="create_by">
          <el-input v-model="activityRemark.create_by" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="最近修改时间" prop="edit_time">
          <el-input v-model="activityRemark.edit_time" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="修改人" prop="edit_by">
          <el-input v-model="activityRemark.edit_by" auto-complete="off"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	export default {
		data() {
			return {
			  query:{
          currentPage: 1,//当前页码
          pageSize:10,//每页显示多少条
          keyword:''//关键字
        },
        pageInfo:{
          total:0,//总数 默认是0
          rows:[],//页面数据
          totalPage:0//页面数据条数
        },
				listLoading: false,
				sels: [],//列表选中列
        //新增和编辑的数据
        activityRemark:{
          id: null,
          name: '',
          preferential_method_id: null,
          start_date: '',
          description: ''
        },
				saveFormVisible: false,//新增或编译界面是否显示
				saveLoading: false,
				saveFormRules: {
					name: [
						{ required: true, message: '请输入标题名字', trigger: 'blur' }
					]
				},
			}
		},
		methods: {
      //性别显示转换
			/*formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.se.x == 0 ? '女' : '未知';
			},*/
			handleCurrentChange(val) {
				this.query.currentPage = val;
				this.getActivityRemarks();
			},
			//获取部门列表
			getActivityRemarks() {
				this.listLoading = true;
				this.$http.post("/activityRemark",this.query)
                  .then(result=>{
                    result = result.data
                    if(result.success){
                      this.pageInfo.total = result.total;
                      // this.activityRemarks = result.data.activityRemarks;
                      this.pageInfo=result.data
                    }else {
                      this.$message({
                        message: '获取分页数据失败！'+result.message,
                        type: 'error'
                      });
                    }
                    this.listLoading = false;
                  })
                  .catch(result=>{
                    console.log("出错了"+result.message)
                  })
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
          let id = row.id;
					this.$http.delete("/activityRemark/"+id)
              .then(result => {
                //NProgress.done();
                result = result.data
                if(result.success){
                  this.$message({
                    message: '删除成功',
                    type: 'success'
                  });
                }else{
                  this.$message({
                    message: '删除失败',
                    type: 'error'
                  });
                }
                this.getActivityRemarks();
                this.listLoading=false
              })
              .catch(result=>{
                this.$message({
                  message: '异常',
                  type: 'error'+result.message
                });
              })
				}).catch(() => {
          this.$message({
            message: '取消成功',
            type: 'error'
          });
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
			  //回显
        this.activityRemark = Object.assign({}, row);
        let path = this.activityRemark.type.path; //完整的path 包含自己的
        if (path){
          let paths = path.split("/"); //根据/分割
          let idsStr = paths.slice(1,paths.length);
          let idsInt = [];
          for (let i = 0; i <idsStr.length ; i++) {//封装成数组
            idsInt.push(parseInt(idsStr[i]));
          }
          // console.log(idsInt);
          //做部门的回显
          this.activityRemark.type = idsInt;//这里部门就变成了一个数组
        }
        this.saveFormVisible = true;
			},
			//显示新增界面
			handleAdd: function () {
			  //清空
        this.activityRemark={
          id: null,
          name: '',
          preferential_method_id: null,
          start_date: '',
          description: ''
        }
				this.saveFormVisible = true;
			},
			//保存
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.saveLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.activityRemark);
              //把数据转换成我们想要格式 parent: [1, 2]---》parent:{id:2} 只需要一个上级部门id 不需要爷爷级的
              if (para.type){
                let typeIds = para.type; // [1, 2]
                let typeId = typeIds[typeIds.length-1]; // 2
                para.type = {"id":typeId} //type:{id:2}
              }
							// para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
							this.$http.put("/activityRemark",para)
                  .then(result => {
                    result = result.data
                    if(result.success){

                      //NProgress.done();
                      this.$message({
                        message: '保存成功',
                        type: 'success'
                      })
                      this.$refs['saveForm'].resetFields();//重置表单
                      this.getActivityRemarks();//保存完重新获取系统数据
                      this.saveFormVisible = false;//关闭对话框
                    }else{
                      this.$message({
                        message: '保存失败',
                        type: 'error'
                      })
                    }
                  })
                  .catch(result=>{
                    this.$message({
                      message: '保存失败',
                      type: 'error'+result.message
                    })
                  });
                this.saveLoading = false;//关闭加载条
							});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
			  let ids=[]//存放选中的id 传入后端的是一个数组
			  for(var i=0;i<this.sels.length;i++){
			    ids.push(this.sels[i].id)
        }
			  // console.log(ids)

				// var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					// let para = {ids: ids};
					this.$http.patch("/activityRemark",ids)
              .then(result => {
                result = result.data
                this.listLoading = false;
                //NProgress.done();
                if(result.success){
                  this.$message({
                    message: '删除成功',
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: '删除失败',
                    type: 'error'+result.message
                  });
                }
                this.getActivityRemarks();
              });
				}).catch(() => {
				});
			}
		},
		mounted() {
			this.getActivityRemarks();
		}
	}

</script>

<style scoped>

</style>